package br.com.controlcivil.webservices;

import java.util.List;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.Dom4JDriver;

import controllcivil.bean.Entity;

public class ImpGeradorXML implements GeradorXML{
	
	private static ImpGeradorXML instance;
	
	private ImpGeradorXML() {
		
	}
	
	public static ImpGeradorXML iniciar(){
		if(instance == null){
			instance = new ImpGeradorXML();
		}
		
		return instance;
	}

	@Override
	public String converterParaXML(Object object) {
		XStream xStream = new XStream();
		xStream.alias(object.getClass().getSimpleName(), object.getClass());
		return xStream.toXML(object);
	}
	
	@Override
	public String converterParaXML(List<Entity> listaObject) {
		XStream xStream = new XStream();
		if(listaObject != null && listaObject.size() > 0 ){
			Object object = listaObject.get(0);
			xStream.alias(object.getClass().getSimpleName()+"s", List.class);
			xStream.alias(object.getClass().getSimpleName(), object.getClass());
			return xStream.toXML(listaObject);
		}
		
		return null;

	}

	@Override
	public Object converterParaEntidade(String nomeEntidade, String xml) {
		return null;
	}
	
	@Override
	public Object converterParaListaEntidade(String nomeEntidade, String xml) {
        XStream xStream = new XStream(new Dom4JDriver());
        List<Object> retorno = (List<Object>) xStream.fromXML(xml);
		return retorno;
	}

}
